基本数据类型及内置方法(二)

您所在的位置:网站首页 reverse 索引 基本数据类型及内置方法(二)

基本数据类型及内置方法(二)

#基本数据类型及内置方法(二)| 来源: 网络整理| 查看: 265

基本数据类型及内置方法(二)

一、列表的内置方法

基本数据类型及内置方法(二)

count:统计当前列表内的指定元素的个数

s1 = ['y', 'a', 'f', 'e', 'n', 'g', '6', '6', '6']

s1.count('6')

print(s1.count('6'))

>>>3

index:获取当前元素的索引,可指定查找范围

s1 = ['y', 'a', 'f', 'e', 'n', 'g', '6', '6', '6']

s1.index('6')

print(s1.index('6'))

>>>6

s1.index("6", 7)

print(s1.index("6", 7))

>>>7

sort:排序,默认reverse=False,也就是升序排序

s1 = [1, 2, 5, 6, 7, 8, 9]

s1.sort()

print(s1)

>>>[1, 2, 5, 6, 7, 8, 9]

s1 = [1, 2, 5, 6, 7, 8, 9]

s1.sort(reverse=True)

print(s1)

>>>[9, 8, 7, 6, 5, 2, 1]

sorted:python内置的排序,,在排序时生产一个新列表,原数据不变

s1 = [1, 2, 5, 6, 7, 8, 9]

a = sorted(s1)

print(a)

>>>[1, 2, 5, 6, 7, 8, 9]

s1 = [1, 2, 5, 6, 7, 8, 9]

a = sorted(s1, reverse=True)

print(a)

>>>[9, 8, 7, 6, 5, 2, 1]

clear:清除列表中的的所有数据

s1 = [1, 2, 5, 6, 7, 8, 9]

s1.clear()

print(s1)

>>>[]

队列与堆栈

'''

队列:先进先出(有出口和入口)

l1 = []

l1.append(1)

l1.append(2)

l1.append(3)

print(l1)

>>>[1,2,3]

l1.pop[0]

print(l1)

>>>[2,3]

l1.pop[0]

print(l1)

>>>[3]

l1.pop[0]

print(l1)

>>>[]

堆栈: 先进后出(类似于箱子只有一个口子)

l1 = []

l1.append(1)

l1.append(2)

l1.append(3)

print(l1)

>>>[1,2,3]

l1.pop()

print(l1)

>>>[1,2]

l1.pop()

print(l1)

>>>[1]

l1.pop()

print(l1)

>>>[]

'''

小结

列表是可以存多个值,有序(有索引就有序),可变

二、元祖

定义及用法

# 用途:存储多个不同类型的值(不能存可变类型)

# 定义方式:用过小括号存储数据,数据与数据之间通过逗号分隔,(值不能被改变)

# 定义容器类型的时候,如果里面只有一个值,在值的后面加上一个逗号(很重要)

# 在元组中如果不加,就是字符串

常用方法

"""

1、索引取值(正取,反取)

2、索引切片

3、成员运算in ,not in

4、len()

"""

1、索引取值

t1 = ('y', 'a', 'f', 'e', 'n', 'g')

print(t1[1])

>>>a

t1 = ('y', 'a', 'f', 'e', 'n', 'g')

print(t1[-2])

>>>n

2、索引切片

t1 = ('y', 'a', 'f', 'e', 'n', 'g')

print(t1[2:])

>>>('f', 'e', 'n', 'g')

3、成员运算

t1 = ('y', 'a', 'f', 'e', 'n', 'g')

print('a' in t1)

>>>True

4、len()

t1 = ('y', 'a', 'f', 'e', 'n', 'g')

print(len(t1))

>>>6

小结

有序,可存多个值,不可变

三、字典

定义及定义方式

定义方式:通过大括号来存储数据,通过key:value来定义键值对数据,每个键值对中间通过逗号分隔

key:一定是一个不可变类型

value:可以是任意类型

三种定义方式:

1、重要

d1 = {

'name': 'yafeng',

'age': '18',

'hobby': 'study'

}

print(d1)

>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study'}

2、重要

d2 = dict(name='yafeng', age='18', hobby='study')

print(d2)

>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study'}

3、了解即可

l1 = ['name', "age", "hobby"]

l2 = ['yafeng', 18, "study"]

z1 = zip(l1, l2)

#zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象。

如果各个可迭代对象的元素个数不一致,则返回的对象长度与最短的可迭代对象相同。

print(dict(z1))

>>>{'name': 'yafeng', 'age': 18, 'hobby': 'study'}

优先掌握的方法

"""

1、优先掌握的:

1、按照key:value映射关系取值(可存可取)

2、成员运算in,not in # 默认判断key

3、len() # 获取当前字典中键值对的个数

"""

内置方法get

get:获取指定key的值,如果值不存在,默认返回None,可以通过第二个参数修改默认返回的内容

d1 = {

'name': 'yafeng',

'age': '18',

'hobby': 'study'

}

print(d1.get('name'))

>>>yafeng

keys、values、items

keys返回所有的key

values返回所有的值

items返回所有的键值对,返回值是列表

d1 = {

'name': 'yafeng',

'age': '18',

'hobby': 'study'

}

for key in d1.keys():

print(key)

for value in d1.values():

print(value)

for key, value in d1.items():

print(key, value)

key, value = ("name", 'age')

>>>name

age

hobby

yafeng

18

study

name yafeng

age 18

hobby study

pop

pop指定key进行删除,有返回值,返回为对应的value

a = d1.pop('age')

print(d1)

print(a)

>>>{'name': 'yafeng', 'hobby': 'study'}

>>>18

popitem

随机弹出一个键值对,有返回值,返回是一个元祖

d1.popitem()

print(d1)

>>>{'name': 'yafeng', 'age': '18'}

update

用新字典替换旧字典

d1.update({'gender': 'male'})

print(d1)

>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study', 'gender': 'male'}

fromkeys

生产一个新字典,以第一个参数(可迭代对象)中的各个元素为key,以第二个参数为值,组成一个新的字典

print(dict.fromkeys([1, 2, 3], ['ke', 'k1']))

>>>{1: ['ke', 'k1'], 2: ['ke', 'k1'], 3: ['ke', 'k1']}

setdefault

key不存在就新增键值对,有返回值,返回新增value,key存在返回对应的value

print(d1.setdefault('height', '1.77'))

print(d1)

>>>1.77

>>>{'name': 'yafeng', 'age': '18', 'hobby': 'study', 'height': '1.77'}

四、集合

# 用途: 去重、关系运算

# 定义方式: 通过大括号存储数据,每个元素通过逗号分隔

# 定义空集合,必须使用set()来定义

# l1 = []

# s1 = ""

# d1 = {}

# ss1 = set()

# 常用方法:

"""

合集:|

交集:&

差集:-

对称差集:^

"""

"""

1、集合中不可能出现两个相同的元素

"""

python_student = {'egon', 'jason', 'tank', 'owen', 'egon'}

linux_student = {'frank', 'alex', 'egon'}

go_student = {'egon'}

print(python_student)

# print(python_student | linux_student)

# print(python_student & linux_student)

# print(python_student - linux_student)

# print(linux_student - python_student)

# print(python_student ^ linux_student)

# print(python_student > go_student)

# print(python_student < linux_student)

# l1 = [1, 2, 3, 1, 2, 9, 1, 5, 6, 7]

# print(l1)

#

# s1 = set(l1)

# print(s1)

# print(type(s1))

# l2 = list(s1)

# print(l2)

# print(type(l2))

for i in python_student:

print(i)

小结

无序,可变,存多个值

五、总结

"""

存一个:整型、浮点型、字符串

存多个值:列表、元组、字典、集合

可变or不可变:

可变:;列表,字典,集合

不可变:整型、浮点型、字符串、元组

有序or无序:

有序:字符串、列表、元组

无序:字典、集合

占用空间:

字典

列表

元组

集合

字符串

数字类型

"""



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3